fetch('/wp-json/custom/v1/server-time')
  .then(response => response.json())
  .then(data => {
    const serverTimestamp = data.timestamp;
    const now = new Date(serverTimestamp * 1000);
    const targetUTCDate = new Date(Date.UTC(2025, 3, 14, 20, 30));
    const expiredSeconds = Math.floor((targetUTCDate - now) / 1000);
    const countdownDiv = document.querySelector(".countdown");

    jQuery(document).ready(function($) {
      FlipClock.Lang.fa = {
        days: "روز",
        hours: "ساعت",
        minutes: "دقیقه",
        seconds: "ثانیه",
      }
      const countdown = $(".countdown").FlipClock(expiredSeconds,{
        clockFace: 'DailyCounter',
        countdown: true,
        language: 'fa',
        callbacks: {
          stop: ()=> {
              countdownDiv.style.opacity = 0;
          }}
      });
      const discoutBtn = document.querySelectorAll(".discount-btn");
      const sideDiscount = document.querySelector(".side-discount");
      discoutBtn.forEach(btn=>{
          btn.addEventListener("click", ()=>{
              if(sideDiscount.classList.contains("active")) {
                  sideDiscount.classList.remove("active")
              } else {
                  sideDiscount.classList.add("active")
              }
          })
      });

      window.addEventListener("load", ()=>{
          setTimeout(()=>{
              sideDiscount.classList.add("active")
          }, 2000)
      });
    });
  })
  .catch(error => {
    console.error("Error fetching server time:", error);
  });
